package com.xiaoq.positionmarket.repository.impl;

import com.xiaoq.positionmarket.entity.PositionPushRecordEntity;
import com.xiaoq.positionmarket.repository.IPositionPushRecordRepository;
import org.apache.commons.lang3.StringUtils;
import org.mongodb.morphia.Datastore;
import org.mongodb.morphia.query.Query;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by chenjh on 2018/4/4.
 */
@Repository
public class PositionPushRecordRepositoryImpl implements IPositionPushRecordRepository {

    @Autowired
    private Datastore datastore;

    @Override
    public boolean save(PositionPushRecordEntity entity) {
        if(entity == null || StringUtils.isBlank(entity.getId())){
            return false;
        }
        datastore.save(entity);
        return true;
    }

    @Override
    public List<PositionPushRecordEntity> getListByPositionMarketId(String positionMarketId) {
        if(StringUtils.isBlank(positionMarketId)){
            return new ArrayList<>();
        }
        Query<PositionPushRecordEntity> query = datastore.createQuery(PositionPushRecordEntity.class)
                .filter("positionMarketId", positionMarketId);
        return query.asList();
    }
}
